package com.jmye.mybatis.dao;

import com.jmye.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * @Description
 * @Date 2024/11/26 下午12:48
 * @Author jmye
 */
public class UserDaoImpl implements UserDao{
    @Override
    public List<User> findAll() throws IOException {
        // 加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 获取 SqlSession 工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 获取 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 执行 SQL 语句
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        // 释放资源
        sqlSession.close();

        return userList;
    }
}
